package c_admin
import (
	"gintest/until"
	"gintest/core/drive"
	"github.com/gin-gonic/gin"
	"strings"
	"strconv"
	"gintest/core/config"
)

func Datadb_list(c *gin.Context){
	page,_:=strconv.ParseUint(c.DefaultPostForm("page","1"),10,64)
	pagesize,_:=strconv.ParseUint(c.DefaultPostForm("limit","15"),10,64)
	var total int
	var list_total []interface{}
	if config.Sconfig.Read("token","type")=="boltdb"{
		var list_total_t []drive.Boltdb
		list_total_t,total=drive.BoltdbReadList("token")
		list_total=make([]interface{},total)
		for i, d := range list_total_t {
			list_total[i] = d
		}
	}else if config.Sconfig.Read("token","type")=="memory"{
		var list_total_t []drive.Memory
		list_total_t,total=drive.MemoryReadList()
		list_total=make([]interface{},total)
		for i, d := range list_total_t {
			list_total[i] = d
		}
	}
	
	if pagesize > uint64(total) {
		pagesize = uint64(total)
	}

	list:=list_total[page-1:page+pagesize-1]
	until.Success(c,map[string]interface{}{"data":list,"total":total})
}
func Datadb_del(c *gin.Context){
	key:=c.DefaultPostForm("key","")
	var err error
	keys:=strings.Split(key,",")

	for i:=0;i<len(keys);i++{
		if config.Sconfig.Read("token","type")=="boltdb"{
			err=drive.BoltdbDel("token",keys[i])
		}else if config.Sconfig.Read("token","type")=="memory"{
			drive.MemoryDel(keys[i])
		}
		if err != nil{
			break
		}
	}
	if err != nil{
		until.Fail(c,1,"删除失败")
	}else{
		until.Success(c,nil)
	}
	
}